﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformDevFramework.Pages
{

    public class ProgressForm : Form
    {
        private BackgroundWorker worker;
        private ProgressBar progressBar;
        private Label lblMessage;
        private Button btnCancel;

        // 公共事件委托
        public delegate void DoWorkEventHandler(object sender, DoWorkEventArgs e);
        public event DoWorkEventHandler DoWork;

        // 公共属性
        public object Argument { get; set; }
        public object Result { get; private set; }

        public ProgressForm(string message = "处理中，请稍候...")
        {
            InitializeComponents();
            lblMessage.Text = message;
        }

        private void InitializeComponents()
        {
            // 窗体设置
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterParent;
            this.ClientSize = new System.Drawing.Size(400, 120);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Text = "进度";

            // 进度条
            progressBar = new ProgressBar();
            progressBar.Style = ProgressBarStyle.Marquee;
            progressBar.Dock = DockStyle.Top;
            progressBar.Height = 30;
            progressBar.MarqueeAnimationSpeed = 30;

            // 消息标签
            lblMessage = new Label();
            lblMessage.Dock = DockStyle.Top;
            lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            lblMessage.Height = 40;
            lblMessage.Padding = new Padding(0, 10, 0, 0);

            // 取消按钮
            btnCancel = new Button();
            btnCancel.Text = "取消";
            btnCancel.Width = 80;
            btnCancel.Height = 30;
            btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnCancel.Location = new System.Drawing.Point(
                this.ClientSize.Width - btnCancel.Width - 20,
                this.ClientSize.Height - btnCancel.Height - 10);
            btnCancel.Click += (s, e) => CancelWork();

            // 添加控件
            this.Controls.Add(btnCancel);
            this.Controls.Add(progressBar);
            this.Controls.Add(lblMessage);

            // 初始化BackgroundWorker
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += Worker_DoWork;
            worker.ProgressChanged += Worker_ProgressChanged;
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
        }

        // 启动工作
        public void StartWork()
        {
            if (!worker.IsBusy)
            {
                this.Show();
                worker.RunWorkerAsync(Argument);
            }
        }

        // 取消工作
        public void CancelWork()
        {
            if (worker.IsBusy)
            {
                worker.CancelAsync();
                btnCancel.Enabled = false;
            }
        }

        // 更新进度
        public void ReportProgress(int percent, string message = null)
        {
            worker.ReportProgress(percent, message);
        }

        // 实际工作方法
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (DoWork != null)
            {
                DoWork(this, e);
            }
            e.Result = e.Result ?? Result;
        }

        // 进度更新处理
        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage >= 0 && e.ProgressPercentage <= 100)
            {
                progressBar.Style = ProgressBarStyle.Continuous;
                progressBar.Value = e.ProgressPercentage;
            }

            if (e.UserState != null)
            {
                lblMessage.Text = e.UserState.ToString();
            }
        }

        // 工作完成处理
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.DialogResult = DialogResult.Abort;
                MessageBox.Show($"操作失败: {e.Error.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (e.Cancelled)
            {
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                Result = e.Result;
            }

            this.Close();
        }
    }

}
